package gateway

import (
	"example.com/m/v2/internal/infrastructure/gateway/middleware"
	"github.com/gin-gonic/gin"
	"sync"
)

var (
	_g    GateWay
	_once sync.Once
)

type GateWay interface {
	GetHttpEngine() *gin.Engine
	Start(addr ...string) error
	Stop()
	Request(args ...any) (any, error)
}

type gateway struct {
	gin *gin.Engine
}

func (g *gateway) Start(addr ...string) error {
	return g.gin.Run(addr...)
}

func (g *gateway) Stop() {
}

func (g *gateway) GetHttpEngine() *gin.Engine {
	return g.gin
}

func (g *gateway) Request(args ...any) (any, error) {
	// 做统一的请求 统一的价签
	//TODO implement me
	panic("implement me")
}

func GetGatewayInstance() GateWay {
	_once.Do(func() {
		gin.DisableConsoleColor()
		//f, _ := os.Create("gin.log")
		//gin.DefaultWriter = io.MultiWriter(f)
		r := gin.New()
		r.Use(gin.Recovery())
		r.Use(gin.Logger())
		r.Use(middleware.LoggerFormatter())
		_g = &gateway{
			gin: r,
		}
	})
	return _g
}
